﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace TextAnalytics.Models
{
    public class ProjectModel
    {
        public string DataDirectoryPath;
        public string RootDirectoryPath;
        public string Name;
        public DirectoryModel RootDirectory;

        public string GetProjectPath()
        {
            return Path.Combine(RootDirectoryPath, "Project.taproj"); 
        }
        public string GetDbPath()
        {
            return Path.Combine(DataDirectoryPath, "Project.DB");
        }

        public void Save()
        {
            var projectPath = GetProjectPath();
            if (string.IsNullOrWhiteSpace(projectPath))
            {
                throw new InvalidOperationException("Cannot save uninitialized project");
            }
            using (var fs = File.OpenWrite(projectPath))
            {
                XmlSerializer ser = new XmlSerializer(this.GetType());
                ser.Serialize(fs, this);
            }
        }

        public static ProjectModel Load(string projectFilePath)
        {
            using(var fs = File.OpenRead(projectFilePath))
            {
                var deser = new XmlSerializer(typeof(ProjectModel));
                return (ProjectModel)deser.Deserialize(fs);
            }
        }
    }

    public class DirectoryModel
    {
        public DirectoryModel()
        {
            Directories = new List<DirectoryModel>();
            Files = new List<FileModel>();
        }

        public string DirectoryPath;
        public static DirectoryModel LoadFromPath(string directoryPath)
        {
            if (!Directory.Exists(directoryPath))
            {
                throw new ArgumentException("directoryPath should be a project directory path");
            }
            var result = new DirectoryModel()
            {
                DirectoryPath = directoryPath
            };
            
            var orderedSubDirectories = Directory.GetDirectories(result.DirectoryPath)
                                                .OrderBy(x => x);
            foreach (var subDirectory in orderedSubDirectories)
            {
                try
                {
                    var subDirModel = DirectoryModel.LoadFromPath(subDirectory);
                    result.Directories.Add(subDirModel);
                }
                catch (Exception ex)
                {
                    AppUtility.DisplayError(ex);
                }
            } 

            result.Files.AddRange(Directory.GetFiles(result.DirectoryPath, "*.txt")
                                            .OrderBy(x => x)
                                            .Select(x => new FileModel(x)));
            return result;
        }
        public string Title { get { return new DirectoryInfo(DirectoryPath).Name; } }
        public List<DirectoryModel> Directories { get; set; }
        public List<FileModel> Files { get; set; }
    }

    public class FileModel
    {
        public FileModel()
        {

        }
        public FileModel(string filePath)
        {
            Path = filePath;
        }
        public string Path { get; set; }
        public string Title { get { return System.IO.Path.GetFileName(Path); } }
    }
}
